<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小工具管理</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://adminlte.io/themes/v3/dist/css/adminlte.min.css" rel="stylesheet">

    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <nav class="main-header navbar navbar-expand navbar-white navbar-light">
        <a class="navbar-brand" href="index.php">后台管理</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
    </nav>

    <aside class="main-sidebar sidebar-dark-primary elevation-4">
        <div class="sidebar">
            <nav class="mt-2">
                <ul class="nav nav-pills nav-sidebar flex-column">
                    <li class="nav-item">
                        <a href="users.php" class="nav-link">用户管理</a>
                    </li>
                    <li class="nav-item">
                        <a href="tools.php" class="nav-link">小工具管理</a>
                    </li>
                    <li class="nav-item">
                        <a href="call-records.php" class="nav-link">调用记录管理</a>
                    </li>
                    <li class="nav-item">
                        <a href="logout.php" class="nav-link">登出</a>
                    </li>
                </ul>
            </nav>
        </div>
    </aside>

    <div class="content-wrapper">
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1>小工具管理</h1>
                    </div>
                </div>
            </div>
        </section>

        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">小工具列表</h3>
                                <button type="button" class="btn btn-primary float-right" onclick="showAddToolModal()">新增小工具</button>
                            </div>
                            <div class="card-body">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th>工具ID</th>
                                            <th>工具名称</th>
                                            <th>描述</th>
                                            <th>调用金额</th>
                                            <th>状态</th>
                                            <th>记录调用</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="toolsTableBody">
                                        <!-- 小工具数据 -->
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <!-- 新增小工具模态框 -->
    <div class="modal fade" id="addToolModal" tabindex="-1" role="dialog" aria-labelledby="addToolModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addToolModalLabel">新增小工具</h5>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="addToolForm">
                        <div class="form-group">
                            <label for="toolName">工具名称</label>
                            <input type="text" class="form-control" id="toolName" name="toolName" required>
                        </div>
                        <div class="form-group">
                            <label for="toolDescription">描述</label>
                            <textarea class="form-control" id="toolDescription" name="toolDescription" required></textarea>
                        </div>
                        <div class="form-group">
                            <label for="toolPrice">调用金额</label>
                            <input type="number" step="0.01" class="form-control" id="toolPrice" name="toolPrice" required>
                        </div>
                        <div class="form-group">
                            <label for="toolStatus">状态</label>
                            <select class="form-control" id="toolStatus" name="toolStatus">
                                <option value="1">上架</option>
                                <option value="0">下架</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="toolLogEnabled">记录调用</label>
                            <select class="form-control" id="toolLogEnabled" name="toolLogEnabled">
                                <option value="1">是</option>
                                <option value="0">否</option>
                            </select>
                        </div>
                        <button type="button" class="btn btn-primary" onclick="addTool()">保存</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 编辑小工具模态框 -->
    <div class="modal fade" id="editToolModal" tabindex="-1" role="dialog" aria-labelledby="editToolModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editToolModalLabel">编辑小工具</h5>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="editToolForm">
                        <input type="hidden" id="toolId" name="toolId">
                        <div class="form-group">
                            <label for="editToolName">工具名称</label>
                            <input type="text" class="form-control" id="editToolName" name="editToolName" required>
                        </div>
                        <div class="form-group">
                            <label for="editToolDescription">描述</label>
                            <textarea class="form-control" id="editToolDescription" name="editToolDescription" required></textarea>
                        </div>
                        <div class="form-group">
                            <label for="editToolPrice">调用金额</label>
                            <input type="number" step="0.01" class="form-control" id="editToolPrice" name="editToolPrice" required>
                        </div>
                        <div class="form-group">
                            <label for="editToolStatus">状态</label>
                            <select class="form-control" id="editToolStatus" name="editToolStatus">
                                <option value="1">上架</option>
                                <option value="0">下架</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="editToolLogEnabled">记录调用</label>
                            <select class="form-control" id="editToolLogEnabled" name="editToolLogEnabled">
                                <option value="1">是</option>
                                <option value="0">否</option>
                            </select>
                        </div>
                        <button type="button" class="btn btn-primary" onclick="editTool()">保存</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            fetchTools();
        });

        function fetchTools() {
            fetch('get-tools.php')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        var tools = data.tools;
                        var tableBody = document.getElementById('toolsTableBody');
                        tableBody.innerHTML = '';
                        tools.forEach(function(tool) {
                            var row = `
                                <tr>
                                    <td>${tool.id}</td>
                                    <td>${tool.name}</td>
                                    <td>${tool.description}</td>
                                    <td>${tool.price}</td>
                                    <td>${tool.status === '1' ? '上架' : '下架'}</td>
                                    <td>${tool.log_enabled === '1' ? '是' : '否'}</td>
                                    <td>
                                        <button type="button" class="btn btn-primary btn-sm" onclick="showEditToolModal(${tool.id})">编辑</button>
                                        <button type="button" class="btn btn-danger btn-sm" onclick="deleteTool(${tool.id})">删除</button>
                                    </td>
                                </tr>
                            `;
                            tableBody.innerHTML += row;
                        });
                    } else {
                        document.getElementById('toolsTableBody').innerHTML = '<tr><td colspan="7">暂无小工具</td></tr>';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }

        function showAddToolModal() {
            var addToolModal = new bootstrap.Modal(document.getElementById('addToolModal'));
            addToolModal.show();
        }

        function addTool() {
            var toolName = document.getElementById('toolName').value;
            var toolDescription = document.getElementById('toolDescription').value;
            var toolPrice = document.getElementById('toolPrice').value;
            var toolStatus = document.getElementById('toolStatus').value;
            var toolLogEnabled = document.getElementById('toolLogEnabled').value;

            fetch('add-tool.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `name=${encodeURIComponent(toolName)}&description=${encodeURIComponent(toolDescription)}&price=${encodeURIComponent(toolPrice)}&status=${encodeURIComponent(toolStatus)}&log_enabled=${encodeURIComponent(toolLogEnabled)}`,
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        var addToolModal = new bootstrap.Modal(document.getElementById('addToolModal'));
                        addToolModal.hide();
                        fetchTools();
                        alert('小工具新增成功');
                    } else {
                        alert('新增失败: ' + data.message);
                    }
                })
                .catch(error => {
                    alert('请求失败: ' + error);
                });
        }

        function showEditToolModal(toolId) {
            fetch(`get-tool.php?id=${toolId}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        var tool = data.tool;
                        document.getElementById('toolId').value = tool.id;
                        document.getElementById('editToolName').value = tool.name;
                        document.getElementById('editToolDescription').value = tool.description;
                        document.getElementById('editToolPrice').value = tool.price;
                        document.getElementById('editToolStatus').value = tool.status;
                        document.getElementById('editToolLogEnabled').value = tool.log_enabled;
                        var editToolModal = new bootstrap.Modal(document.getElementById('editToolModal'));
                        editToolModal.show();
                    } else {
                        alert('获取小工具信息失败: ' + data.message);
                    }
                })
                .catch(error => {
                    alert('请求失败: ' + error);
                });
        }

        function editTool() {
            var toolId = document.getElementById('toolId').value;
            var toolName = document.getElementById('editToolName').value;
            var toolDescription = document.getElementById('editToolDescription').value;
            var toolPrice = document.getElementById('editToolPrice').value;
            var toolStatus = document.getElementById('editToolStatus').value;
            var toolLogEnabled = document.getElementById('editToolLogEnabled').value;

            fetch('edit-tool.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `id=${encodeURIComponent(toolId)}&name=${encodeURIComponent(toolName)}&description=${encodeURIComponent(toolDescription)}&price=${encodeURIComponent(toolPrice)}&status=${encodeURIComponent(toolStatus)}&log_enabled=${encodeURIComponent(toolLogEnabled)}`,
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        var editToolModal = new bootstrap.Modal(document.getElementById('editToolModal'));
                        editToolModal.hide();
                        fetchTools();
                        alert('小工具编辑成功');
                    } else {
                        alert('编辑失败: ' + data.message);
                    }
                })
                .catch(error => {
                    alert('请求失败: ' + error);
                });
        }

        function deleteTool(toolId) {
            if (confirm('确定要删除此小工具吗？')) {
                fetch('delete-tool.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `id=${encodeURIComponent(toolId)}`,
                })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            fetchTools();
                            alert('小工具删除成功');
                        } else {
                            alert('删除失败: ' + data.message);
                        }
                    })
                    .catch(error => {
                        alert('请求失败: ' + error);
                    });
            }
        }
    </script>
</body>
</html>